//
// Created by konglinglong on 2020/9/11.
//

#ifndef O5G_SRC_O5GRAN_L2_INCLUDE_L2DEF_H
#define O5G_SRC_O5GRAN_L2_INCLUDE_L2DEF_H

#include "../../../../lib/csp/include/cspApi.h"
#include "../../include/ranApi.h"
#include "../../lcore/include/commDef.h"
#include "../../lcore/misc/misc.h"

#define RAN_UL_SCH_RSLT_NUM_PER_SLOT_MAX  (128)
#define RAN_DL_SCH_RSLT_NUM_PER_SLOT_MAX  (128)
#define RAN_DL_SCH_RSLT_RECORD_NUM_MAX    (4)

typedef struct RanL2UeInst
{
    uint32_t inUse;
    uint32_t ueId;

    uint32_t nbIdx;
    uint32_t cellIdx;
    uint32_t ueIdx;

} S_RanL2UeInst;

/* MIB */
typedef struct RanBchSchRslt
{
    uint16_t PhysCellId;
    uint8_t  BetaPss;
    uint8_t  SsbBlockIndex;
    uint8_t  SsbSubcarrierOffset;
    uint16_t ssbOffsetPointA;
    uint8_t  bchPayloadFlag;
    uint32_t bchPayload;
} S_RanBchSchRslt;

typedef struct RanPchSchRslt
{
    uint32_t rsv;
} S_RanPchSchRslt;

typedef struct RanUlschSchRslt
{
    uint32_t rsv;
} S_RanUlschSchRslt;

/* SIB */
typedef struct RanDlschSchRslt
{
    uint32_t rsv;
} S_RanDlschSchRslt;

typedef struct RanSchRslt
{
    S_O5gSfnSlot sfnSlot;
    uint16_t bchExist;
    S_RanBchSchRslt bchSchRslt;
    uint16_t pchExist;
    S_RanPchSchRslt pchSchRslt;
    uint16_t ulschNum;
    S_RanUlschSchRslt ulschSchRslt[RAN_UL_SCH_RSLT_NUM_PER_SLOT_MAX];
    uint16_t dlschNum;
    S_RanDlschSchRslt dlschSchRslt[RAN_DL_SCH_RSLT_NUM_PER_SLOT_MAX];
} S_RanSchRslt;

typedef struct Sch2RlcSchRsltItem
{
    uint8_t ueId;
    uint8_t lcid;
    uint16_t idx;
    uint32_t xchId;
    uint32_t schByteSize;
} S_Sch2RlcSchRsltItem;

typedef struct Sch2RlcSchRslt
{
    uint16_t schRsltNum;
    S_Sch2RlcSchRsltItem schRslt[RAN_DL_SCH_RSLT_NUM_PER_SLOT_MAX];
} S_Sch2RlcSchRslt;

typedef struct RanL2CellInst
{
    uint32_t inUse;
    uint32_t cellId;

    uint32_t nbIdx;
    uint32_t cellIdx;

    E_AppState appState;

    pthread_t ulHiTid;
    pthread_t dlHiTid;
    pthread_t ulLoTid;
    pthread_t dlLoTid;
    pthread_t mschSchTid;
    pthread_t mschAlgTid;

    S_OsSelect ulHiTtiSelector;
    S_OsSelect dlHiTtiSelector;
    S_OsSelect ulLoTtiSelector;
    S_OsSelect dlLoTtiSelector;
    S_OsSelect mschSchTtiSelector;
    S_OsSelect mschAlgTtiSelector;

    S_Poller *ulHiPoller;
    S_Poller *dlHiPoller;
    S_Poller *ulLoPoller;
    S_Poller *dlLoPoller;
    S_Poller *mschSchPoller;
    S_Poller *mschAlgPoller;

    S_OsSelect ttiIntSelector;

    uint16_t slotNum;
    S_O5gSfnSlot currAirSfnSlot;
    S_O5gSfnSlot schSfnSlot;

    uint32_t schRsltIdx;
    S_RanSchRslt schRslt[RAN_DL_SCH_RSLT_RECORD_NUM_MAX];

    S_VarRingBuffer *pL2L1CpMsgQue;
    S_VarRingBuffer *pL1L2CpMsgQue;
    S_VarRingBuffer *pL2L1UpMsgQue;
    S_VarRingBuffer *pL1L2UpMsgQue;

    uint8_t mib[3]; // 24bit
    uint32_t mibSize;

    uint32_t ueNum;
    S_RanL2UeInst ueInst[RAN_UE_NUM_MAX];
} S_RanL2CellInst;

typedef struct RanL2NbInst
{
    uint32_t inUse;
    uint32_t nbId;

    uint32_t nbIdx;

    uint32_t cellNum;
    S_RanL2CellInst cellInst[RAN_CELL_NUM_MAX];
} S_RanL2NbInst;

typedef struct RanL2Context
{
    E_AppType appType;
    E_AppState appState;
    S_VarRingBuffer *pL3iL1iMsgQue;
    S_VarRingBuffer *pL1iL3iMsgQue;
    uint32_t nbNum;
    S_RanL2NbInst nbInst[RAN_NODEB_NUM_MAX];
} S_RanL2Context;

extern S_RanL2Context *gpRanL2Context;

#endif //O5G_SRC_O5GRAN_L2_INCLUDE_L2DEF_H
